﻿using System;
using System.Linq;

using MvcCms.Data.DynamicLINQ;
using MvcCms.Service.Models;
using System.Linq.Expressions;
using System.Collections.Generic;

namespace MvcCms.Service.Code.MvcCmsMobileGrid
{
    public static class MvcCmsMobileGridSearch
    {
        //<summary>
        //Dynamic query builder for  MvcCmsMobileGrid filter.
        //</summary>
        public static IQueryable SearchForMvcCmsMobileGrid<T>(this IQueryable<T> query, MvcCmsMobileGridRequest mvcCmsMobileGridRequest, List<string> columnsToSearch)
        {

            #region Search All Fields

            if (mvcCmsMobileGridRequest.ColumnToSearch == "All")
            {
                var predicate = PredicateBuilder.False<T>();
                Expression<Func<T, bool>> predicateToAdd = null;
                foreach (var columnName in columnsToSearch)
                {
                    Type columnType = query.DynamicType(z => z[columnName]);
                    if (columnType == typeof(System.Int32))
                    {
                        try
                        {
                            var intToSearchFor = int.Parse(mvcCmsMobileGridRequest.Query);
                            //for integer we do equals
                            predicateToAdd = query.DynamicWhereForPredicateBuilder(z => z[columnName] == intToSearchFor);
                        }
                        catch { }
                    }
                    else if (columnType == typeof(System.Decimal))
                    {
                        try
                        {
                            var decimalToSearchFor = decimal.Parse(mvcCmsMobileGridRequest.Query);
                            //for decimal we do equals
                            predicateToAdd = query.DynamicWhereForPredicateBuilder(z => z[columnName] == decimalToSearchFor);
                        }
                        catch { }
                    }
                    else if (columnType == typeof(System.DateTime))
                    {
                        try
                        {
                            var dateToSearchFor = DateTime.Parse(mvcCmsMobileGridRequest.Query);
                            var date2 = dateToSearchFor.AddDays(1.0);
                            //for DateTime we trap between
                            predicateToAdd = query.DynamicWhereForPredicateBuilder(z => z[columnName] >= dateToSearchFor.Date
                                && z[columnName] < date2.Date);
                        }
                        catch { }
                    }
                    else if (columnType == typeof(System.String))
                    {   //for string we do contains
                        predicateToAdd = query.DynamicWhereForPredicateBuilder(z => z[columnName].ToLower().Contains(mvcCmsMobileGridRequest.Query.ToLower())); //contains or
                    }
                    else if (columnType == typeof(System.Boolean))
                    {
                        bool isTrue;
                        try
                        {
                            isTrue = bool.Parse(mvcCmsMobileGridRequest.Query);
                            if (isTrue)
                            {
                                predicateToAdd = query.DynamicWhereForPredicateBuilder(z => z[columnName]);
                            }
                            else
                            {
                                predicateToAdd = query.DynamicWhereForPredicateBuilder(z => !z[columnName]);
                            }
                        }
                        catch { }
                    }
                    if (predicateToAdd != null)
                    {
                        predicate = predicate.Or(predicateToAdd);
                    }

                }
                query = query.Where(predicate);
            }
            #endregion

            #region Search Single Field
            else if (!String.IsNullOrEmpty(mvcCmsMobileGridRequest.Query))
            {
                string columnName = mvcCmsMobileGridRequest.ColumnToSearch;
                Type columnType = query.DynamicType(z => z[columnName]);
                if (columnType == typeof(System.Int32))
                {
                    try
                    {
                        var intToSearchFor = int.Parse(mvcCmsMobileGridRequest.Query);
                        //for integer we do equals
                        query = query.DynamicWhere(z => z[columnName] == intToSearchFor);
                    }
                    catch { }
                }
                else if (columnType == typeof(System.Decimal))
                {
                    try
                    {
                        var decimalToSearchFor = decimal.Parse(mvcCmsMobileGridRequest.Query);
                        //for decimal we do equals
                        query = query.DynamicWhere(z => z[columnName] == decimalToSearchFor);
                    }
                    catch { }
                }
                else if (columnType == typeof(System.DateTime))
                {
                    try
                    {
                        var dateToSearchFor = DateTime.Parse(mvcCmsMobileGridRequest.Query);
                        var date2 = dateToSearchFor.AddDays(1.0);
                        //for DateTime we trap between
                        query = query.DynamicWhere(z => z[columnName] >= dateToSearchFor.Date
                            && z[columnName] < date2.Date);
                    }
                    catch { }
                }
                else if (columnType == typeof(System.String))
                {
                    //for string we do contains
                    query = query.DynamicWhere(z => z[columnName].Contains(mvcCmsMobileGridRequest.Query));
                }
                else if (columnType == typeof(System.Boolean))
                {
                    bool isTrue;
                    try
                    {
                        isTrue = bool.Parse(mvcCmsMobileGridRequest.Query);
                        if (isTrue)
                        {
                            query = query.DynamicWhere(z => z[columnName]);
                        }
                        else
                        {
                            query = query.DynamicWhere(z => !z[columnName]);
                        }
                    }
                    catch { }
                }
            }
            #endregion

            return query;
        }
    }
}


